﻿using ICarlifeRespository;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICarlifeService;

namespace CarlifeService
{
    public class BaoZhengJing : BaseServer<Deposit>, IBaoZhengJing
    {
        private readonly IDepository db;
        private readonly IUOfWork uOfWork;
        private readonly IKeHuRepository huRepository;
        private readonly IClienttypeRepository clienttype;

        public BaoZhengJing(IDepository db, IUOfWork uOfWork, IClienttypeRepository clienttype, IKeHuRepository huRepository) : base(db, uOfWork)
        {
            this.db = db;
            this.uOfWork = uOfWork;
            this.clienttype = clienttype;
            this.huRepository = huRepository;
        }

        public List<DepositDTO> DepositShow(DateTime? StartTime, DateTime? EndTime, string? ClientId = "", string? ClientName = "", int ClientTypeId = 0, int BusinessManagementID = 0,int MoneystateID=0)
        {
#pragma warning disable CS8601 // 引用类型赋值可能为 null。
            var list = from x in db.GetAll()
                       join y in clienttype.GetAll()
                         on x.ClientTypeId equals y.Id
                       join a in huRepository.GetAll()
                      on x.ClientId equals a.Id
                       join c in clienttype.GetAll()
                           on x.BusinessManagementID equals c.Id
                       join b in clienttype.GetAll()
                     on x.MoneystateID equals b.Id
                       select new DepositDTO
                       {
                           ClientId = a.ClientId,
                           ClientName = a.ClientName,
                           TypeName = y.TypeName,
                           Count = x.Count,
                           ContractBond = x.ContractBond,
                           AmountRefunded = x.AmountRefunded,
                           RemainingMargin = x.RemainingMargin,
                           BusinessManagementName = c.TypeName,
                           MoneystateName = b.TypeName,
                           RentStartTime=a.RentStartTime,
                           BusinessManagementID=x.BusinessManagementID,
                           MoneystateID=x.MoneystateID,
                           ClientTypeId=x.ClientTypeId
                       };
#pragma warning restore CS8601 // 引用类型赋值可能为 null。
            if (ClientId != null)
            {
#pragma warning disable CS8602 // 解引用可能出现空引用。
                list = list.Where(x => x.ClientId.Contains(ClientId));
#pragma warning restore CS8602 // 解引用可能出现空引用。
            }
            if (ClientName != null)
            {
                list = list.Where(x => x.ClientName.Contains(ClientName));
            }
            if (ClientTypeId != 0)
            {
                list = list.Where(x => x.ClientTypeId == ClientTypeId);
            }
            if (BusinessManagementID != 0)
            {
                list = list.Where(a => a.BusinessManagementID == BusinessManagementID);
            }
            if (MoneystateID != 0)
            {
                list = list.Where(a => a.MoneystateID == MoneystateID);
            }
            if (StartTime != null)
            {
                list = list.Where(x => x.RentStartTime >= Convert.ToDateTime(StartTime));
            }
            if (EndTime != null)
            {
                list = list.Where(x => x.RentStartTime <= (Convert.ToDateTime(EndTime)));
            }
            return list.ToList();
        }


    }
}
